package com.area.cth.service.impl;

import com.area.cth.controller.DealController;
import com.area.cth.mapper.ManageMapper;
import com.area.cth.pojo.Deal;
import com.area.cth.pojo.Manage;
import com.area.cth.service.ManageService;
import com.area.cth.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ManageServiceImpl implements ManageService {
    @Autowired
    private ManageMapper mm;
    @Autowired
    private DealController dealController;

    @Override
    public void addManage(Manage m) {
        m.setCreateTime(LocalDateTime.now());
        m.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        m.setLaunchUser(id);
        mm.addManage(m);
    }

    @Override
    public List<Manage> getReceiveManage() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer uid = (Integer) map.get("id");
        return mm.getReceiveManage(uid);
    }

    @Override
    public List<Manage> getLaunchManage() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer uid = (Integer) map.get("id");
        return mm.getLaunchManage(uid);
    }

    @Override
    public void updateState(String manageState,Integer mId) {
        if (manageState.equals("已同意")) {
            Deal deal = new Deal();
            Manage us=mm.selectUsersByManageId(mId);
            deal.setGoodsId(us.getGoodsId());
            deal.setSeller(us.getReceiveUser());
            deal.setBuyer(us.getLaunchUser());
            deal.setCreateTime(LocalDateTime.now());
            deal.setUpdateTime(LocalDateTime.now());
            deal.setState("未发货");
            dealController.addDeal(deal);
        }
        mm.updateState(manageState,mId);
    }

    @Override
    public void deleteManage(Integer mId) {
        mm.deleteManage(mId);
    }

    @Override
    public Integer agreeCount(Integer gId) {
        return mm.agreeCount(gId);
    }
}
